package com.socene.kotlin.config.security

import org.springframework.security.core.GrantedAuthority
import org.springframework.security.core.authority.SimpleGrantedAuthority
import org.springframework.security.core.userdetails.UserDetails
import java.util.ArrayList

/**
 *Created by: Sorata 17/9/12 上午9:33
 *
 */

class SysUserDetails(name: String="admin", userId: String="1",pwd: String="admin",roles: Array<String> = arrayOf("")): SUser(name,userId,pwd,roles),UserDetails{

    override fun getAuthorities(): MutableCollection<out GrantedAuthority> {
        val list = ArrayList<GrantedAuthority>()
        if (this.roles.isNotEmpty()) {
            this.roles.mapTo(list) { SimpleGrantedAuthority(it) }
        } else {
            list.add(SimpleGrantedAuthority("normal"))
        }
        return list

    }

    override fun isEnabled(): Boolean {
        return true
    }

    override fun getUsername(): String {
        return name
    }

    override fun isCredentialsNonExpired(): Boolean {
        return true
    }

    override fun getPassword(): String {
        return pwd
    }

    override fun isAccountNonExpired(): Boolean {
        return true
    }

    override fun isAccountNonLocked(): Boolean {
        return true
    }
}


open class SUser(var name:String="admin",
            var userId:String="admin",
            var pwd:String="admin",
            var roles:Array<String> = arrayOf(""))